<?php
require_once('Classes/Utils/FileLift/FileLiftProcessor.inc');

class FileLiftPreProcessor extends FileLiftProcessor
{
	protected function __preProcessor($file)
	{
			switch($this->data['cat']) {
				case 'resource_photo':
					$photo = new Imagick($file['tmp_name']);
					
					// create transparent background
					$back = new Imagick();
					$back->newImage(270, 205, new ImagickPixel('transparent'));
					$back->setImageFormat('png');
					
					// resize original photo to fit globe dialog dimensions
					$photo = $this->scalePhoto($photo, 270, 205);
					$photo->setImageFormat('png');
					
					// compose background & photo to fill the size
					$back->compositeImage($photo, $photo->getImageCompose(), (270-$photo->getImageWidth())/2, (205-$photo->getImageHeight())/2);
					$photo->clear();
					$photo->destroy();
					
					$back->writeImage($file['tmp_name']);
					$back->clear();					
					$back->destroy();
					break;
			}
			return $file;		
	}
	
	protected function scalePhoto($photo, $w, $h) {
		if($photo->getImageWidth() > $photo->getImageHeight())
		{
			$photo->scaleImage($w, 0);
			if($photo->getImageHeight() > $h)
				$photo->scaleImage(0, $h);
		}
		else
		{
			$photo->scaleImage(0, $h);
			if($photo->getImageWidth() > $w)
				$photo->scaleImage($w, 0);
		}
		return $photo;
	}
}
?>